Ext.define('vacake.controller.ShoppingEdit', {
    extend: 'Ext.app.Controller',
    shoppingItem: '',
    config: {
        refs: {
            mainView: 'mainView',
            shoppingEditView: 'shoppingEditView',
            backButton: 'shoppingEditView #back_button',
            showNumber: 'shoppingEditView #showNumber',
            reduceButton: 'shoppingEditView #reduce',
            augmentButton: 'shoppingEditView #augment',
            deleteButton: 'shoppingEditView #delete',
            collectButton: 'shoppingEditView #collectButton',
            completeButton: 'shoppingEditView #complete'
        },
        control: {
            backButton: {
                tap: function () {
                    var backid = Ext.Viewport.getInnerItems().length - 2;
                    Ext.Viewport.remove(this.getShoppingEditView(), true);
                    Ext.Viewport.setActiveItem(backid);
                }
            },
            reduceButton: {
                tap: function () {
                    var number = parseInt(this.getShowNumber().getValue());
                    if (number > 1) {
                        this.getShowNumber().setValue(number - 1);
                    }
                }
            },
            augmentButton: {
                tap: function () {
                    var number = parseInt(this.getShowNumber().getValue());
                    this.getShowNumber().setValue(number + 1);
                }
            },
            deleteButton: {
                tap: function () {
                    Ext.Viewport.mask({
                        xtype: 'loadmask',
                        message: '正在删除中...'
                    });
                    Ext.Ajax.request({
                        url: Global.api_url + '/cloud/1/shopping_cart_delete',
                        method: 'POST',
                        params: {
                            cscid: Ext.getCmp('showItemsId').getValue()
                        },
                        scope: this,
                        success: function (response) {
                            Ext.Viewport.unmask();
                            var result = Ext.decode(response.responseText);
                            cordova.exec(function (message) {
                            }, function (error) {
                            }, "Messages", "showMsg", [result.Variables.Message.messagestr]);
                            if (result.Variables.Result.code == 0) {
                                Ext.Viewport.mask({xtype: 'loadmask', message: '加载数据中...'});
                                var backid = Ext.Viewport.getInnerItems().length - 2;
                                Ext.Viewport.remove(this.getShoppingEditView(), true);
                                Ext.Viewport.setActiveItem(backid);
                            } else {
                                return false;
                            }
                        },
                        failure: function () {
                            Ext.Viewport.unmask();
                            cordova.exec(function (message) {
                            }, function (error) {
                            }, "Messages", "showMsg", ['请求失败,服务器维护中...']);
//                            Ext.Msg.alert('请求失败', '服务器维护中....');
                        }
                    });
                }
            },
            collectButton: {
                tap: function () {
                    Ext.Viewport.mask({
                        xtype: 'loadmask',
                        message: '正在收藏中...'
                    });
                    Ext.Ajax.request({
                        url: Global.api_url + '/cloud/1/collect_add',
                        method: 'POST',
                        params: {
                            cuid: window.localStorage.getItem('app_Login_id'),
                            cpid:  Ext.getCmp('showCpId').getValue(),
                            cadid: window.localStorage.getItem('app_district_id')
                        },
                        scope: this,
                        success: function (response) {
                            Ext.Viewport.unmask();
                            var result = Ext.decode(response.responseText);
                            cordova.exec(function (message) {
                            }, function (error) {
                            }, "Messages", "showMsg", [result.Variables.Message.messagestr]);
//                            Ext.Msg.alert(result.Variables.Message.messagestr);
                        },
                        failure: function () {
                            Ext.Viewport.unmask();
                            cordova.exec(function (message) {
                            }, function (error) {
                            }, "Messages", "showMsg", ['请求失败,服务器维护中...']);
//                            Ext.Msg.alert('请求失败', '服务器维护中....');
                        }
                    });
                }
            },
            completeButton: {
                tap: function () {
                    Ext.Viewport.mask({
                        xtype: 'loadmask',
                        message: '更新购物车...'
                    });
                    Ext.Ajax.request({
                        url: Global.api_url + '/cloud/1/shopping_cart_update',
                        method: 'POST',
                        params: {
                            cscid: Ext.getCmp('showItemsId').getValue(),
                            pnumber: parseInt(this.getShowNumber().getValue())
                        },
                        scope: this,
                        success: function (response) {
                            Ext.Viewport.unmask();
                            var result = Ext.decode(response.responseText);
                            cordova.exec(function (message) {
                            }, function (error) {
                            }, "Messages", "showMsg", [result.Variables.Message.messagestr]);
//                            if (result.Variables.Result.code == 0) {
                                Ext.Viewport.mask({xtype: 'loadmask', message: '加载数据中...'});
                                var backid = Ext.Viewport.getInnerItems().length - 2;
                                Ext.Viewport.remove(this.getShoppingEditView(), true);
                                Ext.Viewport.setActiveItem(backid);
//                            } else {
//                                return false;
//                            }
                        },
                        failure: function () {
                            Ext.Viewport.unmask();
                            cordova.exec(function (message) {
                            }, function (error) {
                            }, "Messages", "showMsg", ['请求失败,服务器维护中...']);
//                            Ext.Msg.alert('请求失败', '服务器维护中....');
                        }
                    });
                }
            }
        }
    }

});